package com.micro.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.micro.common.Result;
import com.micro.common.ResultUtils;
import com.micro.disk.service.UserCapacityService;
import com.micro.disk.user.bean.SessionUserBean;
import com.micro.disk.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags="用户")
@RestController
@RequestMapping("/user")
public class UserController {
	@Reference(check=false)
	private UserService userService;

	@ApiOperation(value="更新密码",notes="更新密码")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token", value = "token",dataType = "String",paramType="query",required=true),
			@ApiImplicitParam(name = "newPwd", value = "newPwd",dataType = "String",paramType="query",required=true),
			@ApiImplicitParam(name = "oldPwd", value = "oldPwd",dataType = "String",paramType="query",required=true)
	})
	@PostMapping("/changepwd")
	public Result changePwd(String token,String newPwd,String oldPwd,HttpServletRequest request){
		try{
			String userName = request.getAttribute("account").toString();
			if(!"".equalsIgnoreCase(userName)){
				SessionUserBean ui=userService.findUserWithPwd(userName,oldPwd);
				if(ui!=null){
					//原密码正确，修改新密码
					userService.updateUserPwd(userName,newPwd);
				}else{
					return ResultUtils.error("原密码不正确！");
				}
			}else{
				return ResultUtils.error("登录状态无效！");
			}
		}catch(Exception e){
			return ResultUtils.error("修改失败！");
		}
		return ResultUtils.success("修改成功！",null);
	}
}
